import fs from 'fs'
import minimist from 'minimist'

const argv = minimist(process.argv)
const { prod, test, api = '' } = argv

let mode = 'dev'
if (prod) {
  mode = 'prod'
}
if (test) {
  mode = 'test'
}

console.log(`process.env: --mode=${mode} --api=${api}`)

const defaultApiMap = {
  dev: 'http://127.0.0.1:7001',
  test: 'http://192.168.20.35:8399',
  prod: 'https://api.codequan.com',
}

const modeOptions = {
  env: {
    BUILD_ENV: mode,
    PLATFORM: 'CLIENT',
    OSS_ORIGIN: 'http://oss.admalltech.com/',
    API_ORIGIN: api || defaultApiMap[mode],
    ENCODE_KEY: 'admalltect202106',
    ENCODE_IV: 'admalltect202106',
  },
  axios: {
    baseUrl: api || defaultApiMap[mode],
  },
}
if (mode === 'test') {
  modeOptions.env.IM_ID = '1400615370'
}
if (mode === 'prod') {
  modeOptions.env.IM_ID = '1400616963'
}
console.log(modeOptions)

// 输出新的pm2启动文件
const pm2Config = `
module.exports = {
  apps: [
    {
      name: 'codequan.com',
      exec_mode: 'cluster',
      instances: ${mode === 'prod' ? '2' : 1},
      script: './node_modules/nuxt/bin/nuxt.js',
      args: 'start --${mode}',
    }
  ]
}
`
console.log(pm2Config)
fs.writeFileSync('./ecosystem.config.js', pm2Config)
console.log('%cpm2 config file generator success', 'color: green')

export default {
  server: {
    port: '7001',
    host: '0.0.0.0',
    timing: false,
  },
  head: {
    title: 'codeqaun',
    htmlAttrs: {
      lang: 'en',
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
  },

  css: [],

  plugins: [],

  components: true,

  buildModules: ['@nuxtjs/eslint-module'],
  styleResources: {
    scss: ['./assets/styles/variable.scss'],
  },

  modules: ['@nuxtjs/style-resources'],

  build: {},
  router: {
    extendRoutes(routes, resolve) {
      console.log(routes)
      routes.push({
        name: 'root',
        path: '/',
        component: resolve(__dirname, 'pages/blog/index.vue'),
      })
    },
  },
  ...modeOptions,
}
